//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by 张洪毓 on 15/11/12.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
//新特性页面的图片数量
private let iconCount = 4

class NewFeatureViewController: UICollectionViewController {

    init() {
        //实例化layout
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = kScreenSize
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        //设置分页
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = true
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //手动注册cell
        //注册NewFeatureCell的重用reuseIdentifier
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return iconCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        cell.index = indexPath.item
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.orangeColor() : UIColor.blueColor()
    
        return cell
    }
    //停止滚动的协议方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / kScreenSize.width)
        if page == iconCount - 1 {
//            print(collectionView?.visibleCells())
            if let cell = collectionView?.visibleCells().last as? NewFeatureCell {
                cell.startAnimation()
            }
        }
    }
    
}

class NewFeatureCell: UICollectionViewCell {
    //实例化子视图
    private lazy var iconView: UIImageView = UIImageView()
    //实例化开始按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: nil)
//    private lazy var startButton:UIButton = {
//        let button = UIButton()
//        button.setTitle("开始体验", forState: .Normal)
//        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
//        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
//        button.hidden = true
//        return button
//    }()
    //显示图片的index
    var index: Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
        }
    }
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     //设置UI
    private func setupUI() {
        //添加子视图
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        //设置约束
        iconView.snp_makeConstraints(closure: { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        })
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
        }
        // 添加点击事件
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
//        startButton.hidden = true
    }
    
    //按钮的点击事件
   @objc private func start() {
//        print("开始体验按钮的点击事件")
        NSNotificationCenter.defaultCenter().postNotificationName(appSwitchRootViewcontroller, object: nil)
    }
    
    private func startAnimation() {
//        startButton.hidden = false //在这里设置该参数会导致重用出错
//        startButton.transform = CGAffineTransformIdentity
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.hidden = false
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    }
    
    
}





